<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="客户编号" prop="customer_id">
                        <el-input v-model="customer_code" readonly placeholder="请选择客户" @click="checkCustom"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="项目编号" prop="project_id">
                        <el-input v-model="project_code" readonly placeholder="请选择项目" @click="checkItem"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="客户抬头" prop="open_data_id">
                        <el-input v-model="open_data_name" readonly placeholder="请选择" @click="checkTitle"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="抬头类型" prop="titleType">
                        <el-input v-model="titleType" disabled placeholder="抬头类型"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="开票类型" prop="openType">
                        <el-input v-model="openType" disabled placeholder="开票类型"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="纳税人识别号" prop="taxpayer_num">
                        <el-input v-model="taxpayer_num" disabled placeholder="纳税人识别号"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="开户行" prop="open_bank">
                        <el-input v-model="open_bank" disabled placeholder="开户行"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="开户账号" prop="open_bank_account">
                        <el-input v-model="open_bank_account" disabled placeholder="开户账号" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="开票金额" prop="money">
                        <el-input v-model="form.money" placeholder="开票金额" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="开票主体" prop="company_id">
                        <el-select v-model="form.company_id" placeholder="请选择开票主体" style="width:100%"  clearable>
                            <el-option v-for="item in companyList" :key="item.id" :label="item.company_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="开票内容" prop="invoice_content">
                        <el-select v-model="form.invoice_content" placeholder="请选择开票内容" style="width:100%"  clearable>
                            <el-option label="会议会展服务" :value="0"/>
                            <el-option label="设备租赁" :value="1"/>
                            <el-option label="摄影摄像服务" :value="2"/>
                            <el-option label="广告物料制作" :value="3"/>
                            <el-option label="广告印刷" :value="4"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="上传凭证" prop="voucher">
                <sc-upload-file v-model="form.voucher" :limit="9">
                    <el-button type="primary" size="small">上传文件</el-button>
                </sc-upload-file>
            </el-form-item>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="备注" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="审核状态" prop="status">
                        <el-select v-model="form.status" placeholder="请选择审核状态" style="width:100%"  clearable>
                            <el-option label="待审核" :value="1"/>
                            <el-option label="审核通过" :value="2"/>
                            <el-option label="审核拒绝" :value="3"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="form.phone" placeholder="电话" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="税率" prop="rate">
                        <el-input v-model="form.rate" placeholder="税率" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <cus-list v-if="dialog.custom" ref="customDialog" @success="customBack" @closed="dialog.custom = false"></cus-list><!-- 客户选择 -->
    <item-list v-if="dialog.item" ref="itemDialog" @success="itemBack" @closed="dialog.item=false"></item-list><!-- 项目选择 -->
    <select-title v-if="dialog.title" ref="titleDialog" @success="titleBack" @closed="dialog.title=false" :query="{customer_id:this.form.customer_id}"></select-title><!-- 开票选择 -->
</template>

<script>
import cusList from '@/views/client/list/cusList.vue';
import itemList from '@/views/project/list/itemList.vue';
import SelectTitle from '@/views/project/invoice/selectTitle.vue';
export default {
  components: { cusList,itemList, SelectTitle },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增发票',
				edit: '编辑发票',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                customer_id:"",
                project_id:"",
                open_data_id:"",
                company_id:"",
                money:"",
                voucher:"",
                remark:"",
                status:"",
                phone:"",
                rate:"",
                invoice_content: "",
			},
            customer_code: "",
            project_code: "",
            open_data_name: "",
            titleType: "",
            openType: "",
            taxpayer_num: "",
            open_bank: "",
            open_bank_account: "",
			//验证规则
			rules: {
				customer_id: [{ required: true, message: '请选择客户' }],
				project_id: [{ required: true, message: '请选择项目' }],
				open_data_id: [{ required: true, message: '请选择客户抬头' }],
			},
            dialog:{
                custom:false,
                item:false,
                title:false,
            },
            companyList:[],
		}
	},
	mounted() {
        this.getCompany()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.invoice.edit.post(this.form);
					} else {
						res = await this.$API.project.invoice.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        projectData(data){
            this.form.project_id = data.project_id
            this.project_code = data.project_code
        },
		//表单注入数据
		setData(data) {
            this.form.id = data.id
		},
        // 客户选择
        checkCustom(){
            this.dialog.custom = true
            this.$nextTick(() => {
                this.$refs.customDialog.open('add')
            })
        },
        customBack(data){
            this.form.customer_id = data.id
            this.customer_code = data.apply_code
        },
        // 项目选择
        checkItem(){
            this.dialog.item = true
			this.$nextTick(() => {
				this.$refs.itemDialog.open('add')
			})
        },
        itemBack(data){
            this.form.project_id = data.id
            this.project_code = data.project_code
        },
        // 抬头选择
        checkTitle(){
            if(!this.form.customer_id){
                this.$message.error("请先选择客户")
            }else{
                let data = {
                    id:this.form.customer_id,
                    apply_code:this.customer_code,
                }
                this.dialog.title = true
                this.$nextTick(() => {
                    this.$refs.titleDialog.open('add').setData(data)
                })
            }
        },
        titleBack(data){
            this.form.open_data_id = data.id
            this.open_data_name = data.customer_rise
            this.titleType = data.rise_type == 0 ? "个人" : "单位"
            this.openType = data.open_type==1 ? "增值税纸质专票" : data.open_type==2 ? "增值税纸质普票" : data.open_type==3 ? "增值税电子普票" : "增值税电子专票"
            this.taxpayer_num = data.taxpayer_num
            this.open_bank = data.open_bank
            this.open_bank_account = data.open_bank_account
        },
        async getCompany(){
            var res = await this.$API.finance.company.all.get();
            this.companyList = res.data;
        },
	}
}
</script>

<style></style>
